﻿@using Dignite.Abp.DynamicForms.DateEdit
@inherits FormControlComponentBase<DateEditFormControl,DateEditConfiguration,DateTime?>

<Validation Validator="@ValidateIsRequired">
    <Field>
        <FieldLabel>@Field.DisplayName</FieldLabel>
        <FieldBody>
            <DateEdit TValue="DateTime?" 
                InputMode="(Blazorise.DateInputMode)Enum.Parse(typeof(Blazorise.DateInputMode),FormConfiguration.InputMode.ToString())"
                Max="@FormConfiguration.Max"
                Min="@FormConfiguration.Min"
                Date="@value" 
                DateChanged="@ChangeValueAsync"
            />
            <FieldHelp>@Field.Description</FieldHelp>
        </FieldBody>
    </Field>
</Validation>

@code{
    private DateTime? value = null;

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (Field.Value != null)
        {
            DateTime convertValue;
            if (DateTime.TryParse(Field.Value.ToString(), out convertValue))
            {
                value = convertValue;
            }
        }
    }

    void ValidateIsRequired( ValidatorEventArgs e )
    {
        var value = Convert.ToString(e.Value);
        if (Field.Required)
        {
            e.Status = string.IsNullOrWhiteSpace(value) ? ValidationStatus.Error : ValidationStatus.Success;
        }
    }
}